package yl.hs.bmipfull.utils;

import okhttp3.*;
import org.apache.http.HttpStatus;
import org.apache.http.client.methods.*;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Value;

import java.io.IOException;
import java.util.Map;

/*
 * WebUtil
 * @category
 * @author 谢景广
 * @date 2022-05-12
 */
public class WebUtil {
    @Value("${swagger.enable}")
    public static boolean DevMode;

    public static String requestGet(String url) throws IOException {
        var httpclient = HttpClients.createDefault();
        var result = "";
        HttpGet get = new HttpGet(url);
        try {
            var res = httpclient.execute(get);
            result = getResponseString(res);
        } catch (IOException ee) {
            throw ee;
        } finally {
            if (httpclient != null) {
                httpclient.close();
            }
        }
        return result;
    }

    public static String ActiveRequest(String url, Map<String, Object> params) throws IOException {
        if (DevMode) {
            return null;
        }
        OkHttpClient client = new OkHttpClient().newBuilder().build();
        var result = "";
        try {
            MediaType mediaType = MediaType.parse("application/json");
            RequestBody body = RequestBody.create(mediaType, Tl.toJson(params));
            Request request = new Request.Builder().url(url).method("POST", body).addHeader("Content-Type", "application/json").build();
            Response response = client.newCall(request).execute();
            var date = response.body().string();
            result = date;
        } catch (IOException ee) {
            throw ee;
        } finally {
            if (client != null) {

            }
        }
        return result;
    }

    public static String requestPost(String url, Map<String, Object> params) throws IOException {
        OkHttpClient client = new OkHttpClient().newBuilder().build();
        var result = "";
        try {
            MediaType mediaType = MediaType.parse("application/json");
            RequestBody body = RequestBody.create(mediaType, Tl.toJson(params));
            Request request = new Request.Builder().url(url).method("POST", body).addHeader("Content-Type", "application/json").build();
            Response response = client.newCall(request).execute();
            var date = response.body().string();
            result = date;
        } catch (IOException ee) {
            throw ee;
        } finally {
            if (client != null) {

            }
        }
        return result;
       /*var result = "";
        var httpclient = HttpClients.createDefault();
        HttpPost post = new HttpPost(url);
        System.out.println(String.format("%s-postdata-%s",url,Tl.toJson(params)));
        try {
            StringEntity data = new StringEntity(Tl.toJson(params));
            data.setContentEncoding("utf-8");
            data.setContentType("application/json");
            post.setEntity(data);
            var res = httpclient.execute(post);
            result = getResponseString(res);
        }catch (IOException ee)
        {
            throw ee;
        }
        finally {
            if(httpclient!=null){
                httpclient.close();
            }
        }*/
    }

    public static String requestGpsPost(String url, Map<String, Object> params) throws IOException {
        OkHttpClient client = new OkHttpClient().newBuilder().build();
        var result = "";
        try {
            MediaType mediaType = MediaType.parse("application/json");
            RequestBody body = RequestBody.create(mediaType, Tl.toJson(params));
            Request request = new Request.Builder().url(url).method("POST", body).addHeader("Content-Type", "application/json").addHeader("Authorization",CK.GpsToken).build();
            Response response = client.newCall(request).execute();
            var date = response.body().string();
            result = date;
        } catch (IOException ee) {
            throw ee;
        } finally {
            if (client != null) {

            }
        }
        return result;
    }

    private static String getResponseString(CloseableHttpResponse res) throws IOException {
        var result = "";
        if (res.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
            var resEntity = res.getEntity();
            if (resEntity != null) {
                result = EntityUtils.toString(resEntity);
            }
        }
        return result;
    }

    public static String requestPut(String url, Map<String, Object> params) {
        var httpclient = HttpClients.createDefault();
        HttpPut get = new HttpPut(url);
        return "";
    }

    public static String requestOption(String url, Map<String, Object> params) {
        var httpclient = HttpClients.createDefault();
        HttpOptions get = new HttpOptions(url);
        return "";
    }

    public static String requestPatch(String url, Map<String, Object> params) {
        var httpclient = HttpClients.createDefault();
        HttpPatch get = new HttpPatch(url);
        return "";
    }

    public static String requestDelete(String url, Map<String, Object> params) {
        var httpclient = HttpClients.createDefault();
        HttpDelete get = new HttpDelete(url);
        return "";
    }

    public static String requestHead(String url, Map<String, Object> params) {
        var httpclient = HttpClients.createDefault();
        HttpHead get = new HttpHead(url);
        return "";
    }

    private static void SetHeader(HttpRequestBase request) {
        request.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.4951.64 Safari/537.36");

    }
}
